package com.cl.unipay4j.ali;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.cl.unipay4j.core.IPayConfig;
import lombok.extern.slf4j.Slf4j;

/**
 * 支付宝支付抽象类
 *
 * @author siashan
 * @since v1.0.1
 **/
@Slf4j
public abstract class AbstractAliPay {

    /**
     * 客户端
     */
    protected AlipayClient alipayClient;
    /**
     * 支付配置
     */
    protected AliPayConfig payConfig;

    public AbstractAliPay() {
        payConfig = new AliPayConfig();
        payConfig.setAppid(AliConfiguration.getAppid());
        payConfig.setSignType(AliConfiguration.getSignType());
        payConfig.setNotifyUrl(AliConfiguration.getNotifyUrl());
        payConfig.setFormat(AliConfiguration.getFormat());
        payConfig.setCharset(AliConfiguration.getCharset());
        payConfig.setMerchantPrivateKey(AliConfiguration.getMerchantPrivateKey());
        payConfig.setAlipayPublicKey(AliConfiguration.getAlipayPublicKey());
        payConfig.setGatewayUrl(AliConfiguration.getGatewayUrl());
        initClient();
    }

    public AbstractAliPay(IPayConfig payConfig) {
        this.payConfig = (AliPayConfig) payConfig;
        initClient();
    }


    /**
     * 初始化客户端
     */
    protected void initClient() {
        this.alipayClient = new DefaultAlipayClient(payConfig.getGatewayUrl(),
                payConfig.getAppid(),
                payConfig.getMerchantPrivateKey(),
                payConfig.getFormat(),
                payConfig.getCharset(),
                payConfig.getAlipayPublicKey(),
                payConfig.getSignType());
    }

}
